﻿using System;
using System.IO;
using Newtonsoft.Json;

namespace Missbot.Common
{
    /// <summary>
    /// 通用工具类
    /// </summary>
    public static class CommonUtil
    {
        /// <summary>
        /// 获取第二天零点的时间
        /// </summary>
        /// <returns></returns>
        public static DateTime UntilTommorow()
        {
            return DateTime.Now.AddDays(1).Date;
        }

        public static string NumberFomat(long number)
        {
            return $"{number:N0}";
        }

        public static string NumberFormatWithSymbol(long number)
        {
            return $"{(number >= 0 ? "+" : "-")}{NumberFomat(Math.Abs(number))}";
        }

        /// <summary>
        /// 从json文件中解析数据
        /// </summary>
        /// <param name="jsonName">json文件名</param>
        /// <typeparam name="T">目标类型</typeparam>
        /// <returns>解析后的数据</returns>
        public static T ReadJsonData<T>(string jsonName)
        {
            using var r = new StreamReader($"{AppDomain.CurrentDomain.BaseDirectory}Data/{jsonName}.json");
            var json = r.ReadToEnd();
            return JsonConvert.DeserializeObject<T>(json);
        }

        /// <summary>
        /// 将数值转换为货币显示
        /// </summary>
        /// <param name="value"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static string CurencyFormat(this int value, string mode = "Gold")
        {
            var currency = $"{value:C0}";
            var replaceSymbol = mode switch
            {
                "Gold" => "💰",
                "Diamond" => "💎",
                _ => ""
            };

            return currency.Replace("¥", replaceSymbol);
        }

        /// <summary>
        /// 解析并分割出一个参数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static (string param, string unsolvedStr) SplitParam(this string str)
        {
            var splits = str.Split(' ');
            var param = splits[0];
            return (param, param.Length + 1 >= str.Length ? string.Empty : str[(splits[0].Length + 1)..].Trim());
        }
    }
}